package com.atguigu.day29;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/*
1.创建一个文件夹 lib
2.导入jar dom4j



 */
public class ParseXmlTest {
    @Test
    public void test01() throws DocumentException {
        //1.创建解析对象
        SAXReader reader = new SAXReader();
        //2.将xml文件转为流
        InputStream is = ParseXmlTest.class.getClassLoader().getResourceAsStream("students.xml");
        //3.从流中读取数据
        Document document = reader.read(is);
        //4.获取根节点
        Element rootElement = document.getRootElement();
        //5.获取子节点
        List<Element> elements = rootElement.elements();

        for (Element element : elements) {
            //根据属性名获取属性值
            String idValue = element.attributeValue("id");
            System.out.println("idValue = " + idValue);
            //获取student里面的子节点
            List<Element> eles = element.elements();
            for (Element ele : eles) {
                System.out.println(ele.getName()+","+ele.getText());
            }
        }


    }
}
